package com.atguigu.tingshu.album.api;

import com.atguigu.tingshu.album.service.TrackInfoService;
import com.atguigu.tingshu.album.service.VodService;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.album.TrackInfo;
import com.atguigu.tingshu.query.album.TrackInfoQuery;
import com.atguigu.tingshu.vo.album.AlbumTrackListVo;
import com.atguigu.tingshu.vo.album.TrackInfoVo;
import com.atguigu.tingshu.vo.album.TrackListVo;
import com.atguigu.tingshu.vo.album.TrackStatVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Tag(name = "声音管理")
@RestController
@RequestMapping("api/album")
@SuppressWarnings({"all"})
public class TrackInfoApiController {

	@Autowired
	private TrackInfoService trackInfoService;

	@Autowired
	private VodService vodService;


	@GetMapping("/trackInfo/getTrackStatVo/{trackId}")
	public Result<TrackStatVo> getTrackStatVo(@PathVariable Long trackId){
		TrackStatVo trackStatVo = trackInfoService.getTrackStatVo(trackId);
		return Result.ok(trackStatVo);
	}

	/**
	 * 获取用户声音分集购买支付列表
	 * @param trackId
	 * @return
	 */
	@GetMapping("/trackInfo/findUserTrackPaidList/{trackId}")
	@GuiGuLogin(require = false)
	public Result<List<Map<String, Object>>> findUserTrackPaidList(@PathVariable Long trackId){
		List<Map<String, Object>> map = trackInfoService.findUserTrackPaidList(trackId);
		return Result.ok(map);
	}

	/**
	 * 根据声音ID+声音数量 获取下单付费声音列表
	 * @param trackId
	 * @param trackCount
	 * @return
	 */
	@GetMapping("/trackInfo/findPaidTrackInfoList/{trackId}/{trackCount}")
	public Result<List<TrackInfo>> findPaidTrackInfoList(@PathVariable Long trackId,@PathVariable Integer trackCount){
		List<TrackInfo> trackInfoList = trackInfoService.findPaidTrackInfoList(trackId,trackCount);
		return Result.ok(trackInfoList);
	}

	/**
	 *  声音上传
	 * @param file
	 * @return
	 */
	@PostMapping("/trackInfo/uploadTrack")
	public Result uploadTrack(MultipartFile file){
		Map<String,String> resultMap =  vodService.uploadTrack(file);
		return Result.ok(resultMap);
	}

	/**
	 *  保存声音
	 * @param trackInfoVo
	 * @return
	 */
	@PostMapping("trackInfo/saveTrackInfo")
	@GuiGuLogin
	public Result saveTrackInfo(@RequestBody TrackInfoVo trackInfoVo){
		Long userId = AuthContextHolder.getUserId();
		trackInfoService.saveTrackInfo(trackInfoVo,userId);
		return Result.ok();
	}

	/**
	 * 分页查询声音
	 * @param page
	 * @param limit
	 * @param trackInfoQuery
	 * @return
	 */
	@PostMapping("/trackInfo/findUserTrackPage/{page}/{limit}")
	@GuiGuLogin
	public Result<Page<TrackListVo>> findUserTrackPage(@PathVariable Long page, @PathVariable Long limit, @RequestBody TrackInfoQuery trackInfoQuery){
		Long userId = AuthContextHolder.getUserId();
		trackInfoQuery.setUserId(userId);
		Page<TrackListVo> trackListVoPage = new Page<>(page, limit);
		trackListVoPage = trackInfoService.findUserTrackPage(trackListVoPage,trackInfoQuery);
		return Result.ok(trackListVoPage);
	}

	/**
	 * 根据id查询声音信息
	 * @param id
	 * @return
	 */
	@GetMapping("trackInfo/getTrackInfo/{id}")
	public Result<TrackInfo> getTrackInfo(@PathVariable Long id){
		TrackInfo byId = trackInfoService.getById(id);
		return Result.ok(byId);
	}

	/**
	 * 修改声音信息
	 * @param id
	 * @param trackInfoVo
	 * @return
	 */
	@PutMapping("/trackInfo/updateTrackInfo/{id}")
	public Result updateTrackInfo(@PathVariable Long id,@RequestBody TrackInfoVo trackInfoVo){
		trackInfoService.updateTrackInfo(id,trackInfoVo);
		return Result.ok();
	}

	/**
	 * 删除声音信息
	 */
	@DeleteMapping("/trackInfo/removeTrackInfo/{id}")
	public Result removeTrackInfo(@PathVariable Long id){
		trackInfoService.removeTrackInfo(id);
		return Result.ok();
	}

	/**
	 * 查询专辑声音分页列表
	 * @param albumId
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("trackInfo/findAlbumTrackPage/{albumId}/{page}/{limit}")
	@GuiGuLogin(require = false)
	public Result<Page<AlbumTrackListVo>> findAlbumTrackPage
			(@PathVariable Long albumId,@PathVariable Long page,@PathVariable Long limit){
		Page<AlbumTrackListVo> albumTrackListVoPage = new Page<>(page, limit);

		Long userId = AuthContextHolder.getUserId();

		albumTrackListVoPage = trackInfoService.findAlbumTrackPage(albumTrackListVoPage,albumId,userId);
		return Result.ok(albumTrackListVoPage);
	}



}












